package yxy.game.pm2.command.passbarrier;

import org.redisson.api.RLock;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.passbariestargazing;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Collection;
import java.util.concurrent.TimeUnit;


/****************************************
 * @name CMD_PassBarrierStarsUpgrade
 * @description 斩关夺隘主观星升级
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierStarsUpgrade, classtype = PassBarrierMessage.PassBarrierStarsUpgradeRequest.class)
public class CMD_PassBarrierStarsUpgrade extends Command<PassBarrierMessage.PassBarrierStarsUpgradeRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierStarsUpgradeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        PassBarrierMessage.PassBarrierStarsUpgradeResponse.Builder response = PassBarrierMessage.PassBarrierStarsUpgradeResponse.newBuilder();
        RLock lock = PassbarrierPlayerModule.getRedisLock(PassbarrierPlayerModule.getlockKey(playerCode));
        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);

            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            int id = module.getStarLevelId();//等级

            passbariestargazing data = DataCenter.getData(id + 1, passbariestargazing.class);
            if (data == null) {
                send(messageSource, response, Result.PassBarrierStarLevelMax);//满级不能再升
                return;
            }

            //todo  业务处理(升级逻辑)
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {

                //判断消耗
                Collection<Currency> currencies = Currency.create(data.getCosttype(), data.getCostid(), data.getCostnum());
                try {
                    new CurrencyHandle().cost(playerCode, CCReason.斩关夺隘, currencies);
                } catch (CurrencyNotEnoughException e) {
                    send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));//消费不足
                    return;
                }

                //升级
                module.setStarLevelId(id + 1);
                module.set();

                //todo 返回
                response.setDataId(module.getStarLevelId());
            }

            PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
            PlayTypeHandle.savePassBarrierStars(playerCode, playTypeModule);//提前把战力算到中间缓存
            new BattleHandle().powerUpdate(playerCode);// 战力更新

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierStarsUpgradeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
